﻿using ImperialServer.Data.DAO;
using ImperialServer.Data.Interfaces;
using ImperialUtilities.Exceptions;
using ImperialUtilities.Utilities;
using ImperialWebSite.Content.Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;

namespace ImperialWebSite.Controllers
{
    public class BaseController
    {
        private IRegistryAccess _daoRegistryAccess;
        private EventViewer _logger;

        /// <summary>
        /// contructor de la clase
        /// </summary>
        public BaseController()
        {
            _logger = new EventViewer("ImperialWebSite.Controllers.BaseController");
            _daoRegistryAccess = new RegistryAccess(ConfigurationManager.ConnectionStrings["ImperialConnection"].ConnectionString);
        }

        /// <summary>
        /// Metodo que se comunica con el servidor para obtener todos los roles
        /// </summary>
        /// <returns>tabla con los roles</returns>
        public DataTable SelectAllRoles()
        {
            DataTable roles = null;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "SelectAllRoles"));
                roles = _daoRegistryAccess.SelectAllRoles();
                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                throw ie;
            }
            catch (Exception e)
            {
                _logger.WriteEventViewerLog("SelectAllRoles", e.Message, true);
                throw new ImperialException(Messages.failureTransactionMessage, e);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "SelectAllRoles"));
            }
            return roles;
        }

        /// <summary>
        /// Metodo que se comunica con el servidor para obtener todos las especies
        /// </summary>
        /// <returns>tabla con las especies</returns>
        public DataTable SelectAllSpecies()
        {
            DataTable species = null;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "SelectAllSpecies"));
                species = _daoRegistryAccess.SelectAllSpecies();
                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                throw ie;
            }
            catch (Exception e)
            {
                _logger.WriteEventViewerLog("SelectAllSpecies", e.Message, true);
                throw new ImperialException(Messages.failureTransactionMessage, e);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "SelectAllRoles"));
            }
            return species;
        }

        /// <summary>
        /// Metodo que se comunica con el servidor para obtener todos los estatus
        /// </summary>
        /// <returns>tabla con los estatus</returns>
        public DataTable SelectAllStatuses()
        {
            DataTable statuses = null;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "SelectAllStatuses"));
                statuses = _daoRegistryAccess.SelectAllStatuses();
                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                throw ie;
            }
            catch (Exception e)
            {
                _logger.WriteEventViewerLog("SelectAllStatuses", e.Message, true);
                throw new ImperialException(Messages.failureTransactionMessage, e);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "SelectAllStatuses"));
            }
            return statuses;
        }

        /// <summary>
        /// Metodo que se comunica con el servidor para obtener todos los roles
        /// </summary>
        /// <returns>tabla con los ciudadanos</returns>
        public DataTable SelectAllCitizens()
        {
            DataTable citizens = null;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "SelectAllCitizens"));
                citizens = _daoRegistryAccess.SelectAllCitizens();
                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                throw ie;
            }
            catch (Exception e)
            {
                _logger.WriteEventViewerLog("SelectAllCitizens", e.Message, true);
                throw new ImperialException(Messages.failureTransactionMessage, e);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "SelectAllCitizens"));
            }
            return citizens;
        }
    }

    
}